package view.overlay;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;

import map.GameWorld;
import stat.LevelDerivedStat;
import stat.StatType;
import entity.Avatar;

public class StatsOverlay extends ActiveOverlay {
	
	private Dimension preferredSize;
	private Avatar player;
	
	public StatsOverlay() {
		
		this.preferredSize = new Dimension(0, 0);
		this.player = GameWorld.getInstance().getAvatar();
	}
	
	protected void paint(Graphics g) {
		
		FontRenderContext frc = ((Graphics2D) g).getFontRenderContext();
		Font font = new Font("Helvetica", Font.BOLD, 16);
		
		g.setColor(Color.WHITE);
		TextLayout textLayout = new TextLayout("Health", font, frc);
		textLayout.draw((Graphics2D) g, 15, 20);
		
		
		if (player.getStats().getStat(StatType.HP) < (player.getStats().getStat(StatType.MAXHP) * .25)) {
			Graphics2D g2 = (Graphics2D) g;
			g2.drawRoundRect(24, 24, ((int) player.getStats().getStat(StatType.HP) / player.getStats().getStat(StatType.MAXHP) * 200) - 2, 20, 4, 4);
		}
		
		g.setColor(Color.LIGHT_GRAY);
		g.fillRect(25, 25, 200, 20);
		g.setColor(new Color(153, 0, 0));
		g.fillRect(26, 26, (int) ((double) player.getStats().getStat(StatType.HP) / player.getStats().getStat(StatType.MAXHP) * 200) - 2, 18);
		
		g.setColor(Color.WHITE);
		textLayout = new TextLayout("Mana", font, frc);
		textLayout.draw((Graphics2D) g, 15, 65);
		
		g.setColor(Color.LIGHT_GRAY);
		g.fillRect(25, 70, 200, 20);
		g.setColor(new Color(0, 0, 102));
		g.fillRect(26, 71, (int) ((double) player.getStats().getStat(StatType.MP) / player.getStats().getStat(StatType.MAXMP) * 200) - 2, 18);
		
		g.setColor(Color.WHITE);
		textLayout = new TextLayout("Experience", font, frc);
		textLayout.draw((Graphics2D) g, 15, 110);
		
		LevelDerivedStat lds = (LevelDerivedStat) player.getStats().getStatObject(StatType.LEVEL);
		int oldExp = lds.getPrevious();
		int newExp = lds.getNext();
		int currExp = player.getStats().getStat(StatType.EXP);
		
		double pixWidth = 0;
		if(currExp > newExp) {
			pixWidth = 200;
		}
		else {
			pixWidth = (((double) currExp - ((double) oldExp)) / ((double) newExp - (double) oldExp) * 200);
		}
		
		g.setColor(Color.LIGHT_GRAY);
		g.fillRect(25, 115, 200, 20);
		g.setColor(new Color(0, 153, 0));
		g.fillRect(26, 116, (int) pixWidth - 2, 18);
		
		int lives = player.getStats().getStat(StatType.LIVES);
		int level = player.getStats().getStat(StatType.LEVEL);
		
		g.setColor(Color.WHITE);
		textLayout = new TextLayout("Lives: " + lives + " / Level: " + level, font, frc);
		textLayout.draw((Graphics2D) g, 15, 155);
	}
	
	public Dimension getPreferredSize() {
		
		return this.preferredSize;
	}
}
